package com.huilian.iotbox.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.huilian.iotbox.admin.service.MeiTuanService;
import com.huilian.tool.common.utils.HuiLianHttpUtil;
import com.huilian.tool.common.utils.MeiTuanSignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;


@Service
@Slf4j
public class MeiTuanServiceImpl implements MeiTuanService {


    /**
     * 授权回调，并换取Token
     * @Author xuxiaoning
     * @Date 2024/8/16
     *
     * @param request
     * @return java.lang.String
     */
    @Override
    public String meituanGeneralAuthCallbacks(HttpServletRequest request) throws Exception {
        log.info("进入meituanGeneralAuthCallbacks美团授权回调");
        String code = request.getParameter("code");
        String sign = request.getParameter("sign");
        String state = request.getParameter("state");
        String developerId = request.getParameter("developerId");
        String businessId = request.getParameter("businessId");
        log.info("code:{}",code);
        log.info("sign:{}",sign);
        log.info("state:{}",state);
        log.info("developerId:{}",developerId);
        log.info("businessId:{}",businessId);


        //使用code换取access_token(即appAuthToken)
        String url = "https://api-open-cater.meituan.com/oauth/token";

        HashMap<String,String> param = new HashMap<>();
        param.put("businessId",businessId);
        param.put("charset","UTF-8");
        param.put("code",code);
        param.put("developerId",developerId);
        param.put("grantType","authorization_code");
        param.put("timestamp",String.valueOf(System.currentTimeMillis() / 1000));

        String SignKey = "ooi21vc5raj5omic";//开发者signKey先写死 后续再看放那
        param.put("sign", MeiTuanSignUtil.getSign(SignKey,param));
        log.info("请求参数param:{}",param);
        JSONObject resultJSON = JSONObject.parseObject(HuiLianHttpUtil.httpPostMap(url, param));
        log.info("使用code换取access_token(即appAuthToken)");
        log.info("结果:{}",resultJSON.toString());
        if (resultJSON.get("code").equals(0)){
            log.info("换取appAuthToken成功");
            JSONObject dataJson = resultJSON.getJSONObject("data");
            log.info("data:{}",dataJson.toString());
            return "美团授权回调成功";
        }else{
            log.info("换取appAuthToken失败");
            log.info("失败code:{}",resultJSON.get("code"));
            log.info("失败msg:{}",resultJSON.get("msg"));
            return "美团授权回调失败";
        }
    }

    /**
     * 取消授权回调
     * @Author xuxiaoning
     * @Date 2024/8/19
     *
     * @param request
     * @return java.lang.String
     */
    @Override
    public String meituanGeneralUnauthCallbacks(HttpServletRequest request) throws Exception {
        log.info("进入meituanGeneralUnauthCallbacks美团取消授权回调");
        String msgType = request.getParameter("msgType");
        String developerId = request.getParameter("developerId");
        String sign = request.getParameter("sign");
        String msgId = request.getParameter("msgId");
        String message = request.getParameter("message");
        String timestamp = request.getParameter("timestamp");
        log.info("code:{}",msgType);
        log.info("sign:{}",developerId);
        log.info("state:{}",sign);
        log.info("developerId:{}",msgId);
        log.info("businessId:{}",message);
        log.info("businessId:{}",timestamp);

        //返回code=0代表接收成功
        HashMap<String,String> map = new HashMap<>();
        map.put("code","0");
        return map.toString();
    }
}
